---
category: Validator
created: '2020-04-19'
title: Check if a number is a power of 2
updated: '2021-10-13'
---

**JavaScript version**

```js isPowerOfTwo.js
const isPowerOfTwo = (n) => (n & (n - 1)) === 0;
```

**TypeScript version**

```ts isPowerOfTwo.ts
const isPowerOfTwo = (n: number): boolean => (n & (n - 1)) === 0;
```

**Examples**

```js examples.js
isPowerOfTwo(256); // true
isPowerOfTwo(129); // false
```
